home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / fbm12s.lha / flrdfb.c < prev    next >
C/C++ Source or Header  |  1994-07-18  |  5KB  |  169 lines

  1. /*****************************************************************
  2.  * flrdfb.c: FBM Release 1.0 25-Feb-90 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989,1990 by Michael Mauldin.  Permission is granted
  5.  * to use this file in whole or in part for any purpose, educational,
  6.  * recreational or commercial, provided that this copyright notice
  7.  * is retained unchanged.  This software is available to all free of
  8.  * charge by anonymous FTP and in the UUNET archives.
  9.  *
  10.  * flrdfb.c: 
  11.  *
  12.  * CONTENTS
  13.  *    read_fbm (image, rfile, mstr, mlen)
  14.  *    read_hdr_fbm (image, rfile, mstr, mlen)
  15.  *
  16.  * EDITLOG
  17.  *    LastEditDate = Mon Jun 25 00:17:51 1990 - Michael Mauldin
  18.  *    LastFileName = /usr2/mlm/src/misc/fbm/flrdfb.c
  19.  *
  20.  * HISTORY
  21.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  22.  *    Package for Release 1.0
  23.  *
  24.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  25.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  26.  *
  27.  * 12-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  28.  *    Created.
  29.  *****************************************************************/
  30.  
  31. # include <stdio.h>
  32. # include <math.h>
  33. # include <ctype.h>
  34. # include "fbm.h"
  35.  
  36. /****************************************************************
  37.  * read_fbm: Read an FBM format bitmap into memory
  38.  ****************************************************************/
  39.  
  40. #ifndef lint
  41. static char *fbmid =
  42. "$FBM flrdfb.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  43. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  44. #endif
  45.  
  46. read_fbm (image, rfile, mstr, mlen)
  47. register FBM *image;
  48. register FILE *rfile;
  49. char *mstr;
  50. int mlen;
  51. { register int j, k, rowlen, plnlen;
  52.   register unsigned char *bmptr;
  53.  
  54.   if (!read_hdr_fbm (image, rfile, mstr, mlen))
  55.   { return (0); }
  56.  
  57.   alloc_fbm (image);
  58.  
  59.   rowlen = image->hdr.rowlen;
  60.   plnlen = image->hdr.plnlen;
  61.  
  62.   if (image->hdr.clrlen >  0)
  63.   { if (! fread (image->cm, image->hdr.clrlen, 1, rfile))
  64.     { fprintf (stderr, "can't read colormap (%d bytes)\n", image->hdr.clrlen);
  65.       return (0);
  66.     }
  67.   }
  68.  
  69.   for (k=0; k<image->hdr.planes; k++)
  70.   { bmptr = &(image->bm[k*plnlen]);
  71.  
  72.     for (j=0; j < image->hdr.rows; j++, bmptr += rowlen)
  73.     { if (fread (bmptr, 1, rowlen, rfile) != rowlen)
  74.       { fprintf (stderr, "premature EOF on input\n");
  75.         return (0);
  76.       }
  77.     }
  78.   }
  79.     
  80.   return (1);
  81. }
  82.  
  83. /****************************************************************
  84.  * read_hdr_fbm: Read an FBM format bitmap header into memory
  85.  ****************************************************************/
  86.  
  87. read_hdr_fbm (image, rfile, mstr, mlen)
  88. register FBM *image;
  89. register FILE *rfile;
  90. char *mstr;
  91. int mlen;
  92. { FBMFILEHDR file_hdr;
  93.   register char *hp;
  94.  
  95.   hp = (char *) &file_hdr;
  96.  
  97.   if (mlen > 0) strncpy (hp, mstr, mlen);
  98.  
  99.   if (! fread (hp+mlen, sizeof (file_hdr) - mlen, 1, rfile))
  100.   { perror ("read_fbm (header)"); return (0); }
  101.  
  102.   if (strncmp (FBM_MAGIC, file_hdr.magic, 2) != 0)
  103.   { fprintf (stderr, "Bad magic number, input is not an FBM image\n");
  104.     return (0);
  105.   }
  106.  
  107.   image->hdr.cols = atoi (file_hdr.cols);
  108.   image->hdr.rows = atoi (file_hdr.rows);
  109.   image->hdr.planes = atoi (file_hdr.planes);
  110.   image->hdr.bits = atoi (file_hdr.bits);
  111.   image->hdr.physbits = atoi (file_hdr.physbits);
  112.   image->hdr.rowlen = atoi (file_hdr.rowlen);
  113.   image->hdr.plnlen = atoi (file_hdr.plnlen);
  114.   image->hdr.clrlen = atoi (file_hdr.clrlen);
  115.   image->hdr.aspect = atof (file_hdr.aspect);
  116.   strncpy (image->hdr.title, file_hdr.title, FBM_MAX_TITLE);
  117.   strncpy (image->hdr.credits, file_hdr.credits, FBM_MAX_TITLE);
  118.     
  119.   if (image->hdr.cols < 1 || image->hdr.cols > 32767)
  120.   { fprintf (stderr, "Invalid number of cols (%d) on input\n",
  121.          image->hdr.cols);
  122.     return (0);
  123.   }
  124.  
  125.   if (image->hdr.rows < 1 || image->hdr.rows > 32767)
  126.   { fprintf (stderr, "Invalid number of rows (%d) on input\n",
  127.          image->hdr.rows);
  128.     return (0);
  129.   }
  130.  
  131.   if (image->hdr.planes != 1 && image->hdr.planes != 3)
  132.   { fprintf (stderr, "Invalid number of planes (%d) on input %s\n",
  133.          image->hdr.planes, "(must be 1 or 3)");
  134.     return (0);
  135.   }
  136.  
  137.   if (image->hdr.bits < 1 || image->hdr.bits > 8)
  138.   { fprintf (stderr, "Invalid number of bits (%d) on input %s\n",
  139.          image->hdr.bits, "(must be [1..8])");
  140.     return (0);
  141.   }
  142.  
  143.   if (image->hdr.physbits != 1 && image->hdr.physbits != 8)
  144.   { fprintf (stderr, "Invalid number of physbits (%d) on input %s\n",
  145.          image->hdr.physbits, "(must be 1 or 8)");
  146.     return (0);
  147.   }
  148.  
  149.   if (image->hdr.rowlen < 1 || image->hdr.rowlen > 32767)
  150.   { fprintf (stderr, "Invalid row length (%d) on input\n",
  151.          image->hdr.rowlen);
  152.     return (0);
  153.   }
  154.  
  155.   if (image->hdr.planes > 1 && image->hdr.plnlen < 1)
  156.   { fprintf (stderr, "Invalid plane length (%d) on input\n",
  157.          image->hdr.plnlen);
  158.     return (0);
  159.   }
  160.  
  161.   if (image->hdr.aspect < 0.01 || image->hdr.aspect > 100.0)
  162.   { fprintf (stderr, "Invalid aspect ratio %lg on input\n",
  163.          image->hdr.aspect);
  164.     return (0);
  165.   }
  166.  
  167.   return (1);
  168. }
  169.